//
//  TabViewController.m
//  FengyuanXS
//
//  Created by 王冬 on 2020/7/25.
//  Copyright © 2020 王冬. All rights reserved.
//

#import "TabViewController.h"
#import "HomeViewController.h"
#import "AppealViewController.h"
#import "MineViewController.h"
#import "RootViewController.h"
@interface TabViewController ()<UITabBarControllerDelegate>
@property (nonatomic, strong) HomeViewController *homeVC;
@property (nonatomic, strong) AppealViewController *classVC;
@property (nonatomic, strong) MineViewController *mineVC;
@end

@implementation TabViewController

- (void)viewDidLoad {
    [super viewDidLoad];
      self.tabBar.tintColor = [UIColor redColor];
      [self setUpAllChildViewController];
}


- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    //NSLog(@" --- %@", item.title);
   
}

/**
 *  添加所有子控制器方法
 */
- (void)setUpAllChildViewController{
    // 1.添加第一个控制器
    HomeViewController *oneVC = [HomeViewController loadStroyboard:[HomeViewController new]];
    [self setUpOneChildViewController:oneVC image:[UIImage imageNamed:@"home_n"]selectImage:[UIImage imageNamed:@"home_s"] title:@""];
    
    // 2.添加第2个控制器
     AppealViewController*twoVC = [AppealViewController  loadStroyboard:[AppealViewController new]];
    [self setUpOneChildViewController:twoVC image:[UIImage imageNamed:@"fabu_n"]selectImage:[UIImage imageNamed:@"fabu_s"] title:@""];
    
    // 3.添加第3个控制器
    MineViewController *fiveVC = [MineViewController loadStroyboard:[MineViewController new]];
    [self setUpOneChildViewController:fiveVC image:[UIImage imageNamed:@"my_n"]selectImage:[UIImage imageNamed:@"my_s"]  title:@""];
    self.delegate = self;
}

/**
 *  添加一个子控制器的方法
 */
- (void)setUpOneChildViewController:(UIViewController *)viewController image:(UIImage *)image
                        selectImage:(UIImage *)selectImage title:(NSString *)title{
    
    UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:viewController];
    navC.title = title;
    navC.tabBarItem.image = image;
    navC.tabBarItem.imageInsets = UIEdgeInsetsMake(0, 0, - 5, 0);
    navC.tabBarItem.selectedImage = selectImage;
    [navC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateSelected];
    // [navC.navigationBar setBackgroundImage:[UIImage imageNamed:@"commentary_num_bg"] forBarMetrics:UIBarMetricsDefault];
    
    //viewController.navigationItem.title = title;
    
    [self addChildViewController:navC];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    return  YES;
     
    
}




//- (void)InitView
//{
//    NSArray *titles = @[@"首页", @"提请诉求", @"我的"];
//    NSArray * selectedImages  = @[@"home_s", @"class_s", @"mine_s"];
//    NSArray *images = @[@"home", @"class", @"mine"];
//    HomeViewController * homeVc = [HomeViewController loadStroyboard:[HomeViewController new]];
//    self.homeVC = homeVc;
//    AppealViewController * classVc = [[AppealViewController alloc] init];
//    self.classVC = classVc;
//    MineViewController * mineVc = [MineViewController loadStroyboard:[MineViewController new]];
//    self.mineVC = mineVc;
//    NSArray *viewControllers = @[homeVc, classVc, mineVc];
//    for (int i = 0; i < viewControllers.count; i++)
//    {
//        UIViewController *childVc = viewControllers[i];
//        [self setVC:childVc title:titles[i] image:images[i] selectedImage:selectedImages[i]];
//    }
//}
//
//- (void)setVC:(UIViewController *)VC title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
//{
//    VC.tabBarItem.title = title;
//    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//    dict[NSForegroundColorAttributeName] = [UIColor blackColor];
//    dict[NSFontAttributeName] = [UIFont systemFontOfSize:10];
//    [VC.tabBarItem setTitleTextAttributes:dict forState:UIControlStateNormal];
//    [VC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"FF5501"]} forState:UIControlStateSelected];
//
//
//    VC.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    VC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    RootViewController *nav = [[RootViewController alloc] initWithRootViewController:VC];
//    [self addChildViewController:nav];
//}


@end
